// mobile bind
// ============

define(['zepto', 'sui'], function($){
  return {
    init: function(data) {
      
      this.coach = data.coach;
      this.start = data.start;
      this.day = data.day;
      this.limit = parseInt(data.limit);
      this.restSum = parseInt(data.restSum);
      this.sum = parseInt(data.restSum);
      
      this.bindClick();
      
      this.bindArrangeSubmit();
      
      this.bindCancelSubmit();
      
      this.validate();
    },
    
    bindClick: function() {
        
        var that = this
        $(document).on("click",".add-rest-content .col-25 a",function (e) {
            
            e.preventDefault();
            
            if($(this).hasClass("disabled")) {
            	return false;
            }
            
            if($(this).hasClass("rest") || $(this).hasClass("experience")) {
              
              if($(this).hasClass("disable")) {
                $(this).removeClass("disable");
              }else {
                $(this).addClass("disable");
              }
            } else {
              if($(this).hasClass("arrange")) {
                $(this).removeClass("arrange");
              }else {
                $(this).addClass("arrange");
              }
            }
            
        })
    },
    
    calculateRestSum: function(type) {
      
      var that = this;
      
      if(that.coach != 13 && type === 'rest') {
        that.restSum = that.restSum + parseInt($('.arrange').length);
      }
      
    },
    
    revokeCalculateRestSum: function(type) {
        
        var that = this;
        
        if(that.coach != 13 && type === 'rest') {
          that.restSum = that.restSum - parseInt($('.arrange').length);
        }
        
      },
    
    validate: function(type) {
      var that = this;
      if(type === 'rest' && that.coach !== 13) {
        if(that.limit < that.restSum) {
          return false;
        }else {
          return true;
        }
      }
      return true;
    },
    
    bindArrangeSubmit: function() {
      var that = this
      $(document).on("click","a.arrange-rest",function (e) {
        e.preventDefault();
        
        var type = $(this).data("type");
        
        if(type === 'rest' && $('.avaliable-hour .col-25 a').hasClass("duty-calendar arrange")) {
        	$.alert("标红时间您在值班，不可选为休息时间，请重新选择")
        	return false;
        }
        
        var arrangeList = that.arrangeList();
        
        if(arrangeList === '') {
          return $.toast("请选择要添加的时间!");
        }
        
        that.calculateRestSum(type);
        if(!that.validate(type)) {
          $.toast("所选时间超出限制，即将清空选择!");
          that.revokeCalculateRestSum(type);
          $(".avaliable-hour .col-25 a").removeClass("arrange");
        } else {
          that.submitArrange(arrangeList, type);
        }
      })
    },
    
    bindCancelSubmit: function() {
        var that = this
        $(document).on("click","a.cancel-rest",function (e) {
          e.preventDefault();
          
          var type = $(this).data("type");
          var disableList = that.disableList(type);
          
          if(disableList === '') {
            return $.toast("请选择要删除的时间!");
          }
          that.submitCancel(disableList, type);
        })
      },
    
    submitArrange: function(arrangeList, type) {
      
      var that = this;
      
      var deffer = $.ajax({  
        url: window.__CTX__.root + "/rest-calendars/arrange",
        data: {
          "coachRestType" : type,
          "day" : that.day,
          "arrangeHours": arrangeList,
          "restLimit" : that.limit,
          "restSum" : that.sum
        },
        type: 'POST',  
        dataType: 'json',
        timeout: 1000,  
        cache: false
      })
      
      $.showPreloader('正在提交修改')
      
      deffer.done(function(response){
        $.hidePreloader()
        if(response.code == 0) {
          $.toast('修改休息时间成功')
          window.location.reload();
        }else {
          $.toast(response.message);
          $(".avaliable-hour .col-25 a").removeClass("arrange");
        }
      })
      
    },
    
    submitCancel: function(disableList, type) {
        
        var that = this;
        
        var deffer = $.ajax({  
          url: window.__CTX__.root + "/rest-calendars/cancel",
          data: {
            "coachRestType" : type,
            "day" : that.day,
            "disableHours": disableList
          },
          type: 'POST',  
          dataType: 'json',
          timeout: 1000,  
          cache: false
        })
        
        $.showPreloader('正在提交修改')
        
        deffer.done(function(response){
          $.hidePreloader()
          if(response.code == 0) {
            $.toast('修改休息时间成功')
            window.location.reload();
          }else {
            $.toast(response.message);
            $("." + type + "-hour .col-25 a").removeClass("disable");
          }
        })
        
      },
    
    arrangeList: function() {
      var arrangeList = "";
      $('.avaliable-hour .col-25 a').each(function() {
        if($(this).hasClass('arrange')) {
        	arrangeList = arrangeList + $(this).html() + ",";
        }
      }) 
      return arrangeList.replace(/,+$/, '');
    },
    
    disableList: function(type) {
      var disableList = "";
      $('.' + type + '-hour .col-25 a').each(function() {
        if($(this).hasClass('disable')) {
          disableList = disableList + $(this).html() + ",";
        }
      }) 
      return disableList.replace(/,+$/, '');
    }
    
  }

})